home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / rs0422.zip / LEVEL3 / SORTSYM.C < prev    next >
C/C++ Source or Header  |  1988-11-04  |  4KB  |  252 lines

  1. #include <string.h>
  2. #undef NULL
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #define TRUE 1
  6. #define FALSE 0
  7.  
  8. FILE *fnami, *fnamo;
  9. char zarr[100],fname[80];
  10.  
  11. #define STRMAX 10
  12. #define STRLEN 7
  13.  
  14. struct inrec {
  15.     unsigned int val;
  16.     char name[STRMAX];
  17. };
  18.  
  19. #define SORTMAX 1000
  20.  
  21. struct inrec sort_tab[SORTMAX];
  22. int key_tab[SORTMAX];
  23. int tabmax;
  24.  
  25. int
  26. getlin(str,max)
  27. char *str;
  28. int max;
  29. {
  30.     int ch, pos;
  31.  
  32.     ch=pos=0;
  33.     while (ch != 0x0a && pos<max) {
  34.         ch=getchar();
  35.         if (ch == -1) return -1;
  36.         str[pos++]=toupper(ch);
  37.         str[pos]=0;
  38.     }
  39.     if (ch== 0x0a) str[pos-1]=0;
  40.     return (strlen(str));
  41. }
  42.  
  43. int
  44. fgetlin(str,max,f)
  45. char *str;
  46. int max;
  47. FILE *f;
  48. {
  49.     int ch, pos;
  50.  
  51.     ch=pos=0;
  52.     while (ch != 0x0a && pos<max) {
  53.         ch=getc(f);
  54.         if (ch == -1) return -1;
  55.         str[pos++]=toupper(ch);
  56.         str[pos]=0;
  57.     }
  58.     if (ch== 0x0a) str[pos-1]=0;
  59.     return (strlen(str));
  60. }
  61.  
  62. int
  63. fgetline(str,max,f)
  64. char *str;
  65. int max;
  66. FILE *f;
  67. {
  68.     int l;
  69.  
  70.     while ((l=fgetlin(str, max, f)) == 0) ;
  71.     return l;
  72. }
  73.  
  74. int
  75. getline(str,max)
  76. char *str;
  77. int max;
  78. {
  79.     int l;
  80.  
  81.     while ((l=getlin(str, max)) == 0) ;
  82.     return l;
  83. }
  84.  
  85. int
  86. fputline(s,f)
  87. char *s;
  88. FILE *f;
  89. {
  90.     while (*s) fputc(*s++,f);
  91.     fputc(0x0a,f);
  92. }
  93.  
  94. int
  95. get_yes()
  96. {
  97.     register char ch;
  98.  
  99.     do {
  100.         ch=getch();
  101.         ch=toupper(ch);
  102.     } while ( (ch!=0x59) && (ch!=0x4e));
  103.     return (ch == 0x59); /* True is Yes */
  104. }
  105.  
  106. putxch(val, f)
  107. FILE f;
  108. int val;
  109. {
  110.     char v;
  111.  
  112.     v= (val >> 4) &0x0f;
  113.     if (v > 9) v += 7;
  114.     fputc(v+0x30, f);
  115.     v=(val & 0x0f);
  116.     if (v > 9) v += 7;
  117.     fputc(v+0x30, f);
  118. }
  119.  
  120. unsigned int
  121. gethex(ch)
  122. char **ch;
  123. {
  124.     unsigned int v;
  125.     int i,j;
  126.     register char *c;
  127.  
  128.     c = *ch;
  129.     v = 0;
  130.     while (*c == 0x20) c++;
  131.     while (*c) {
  132.         i = toupper(*c++);
  133.         v = v<<4;
  134.         if (i >=0x30 && i <= 0x39) v += (i&0x0f);
  135.         else if (i >= 0x41 && i <= 0x46) v += (9 + (i&0x0f));
  136.         else break;
  137.         if (*c == 0x20) break;
  138.     }
  139.     while (*c == 0x20) c++;
  140.     *ch = c;
  141.     return v;
  142. }
  143.  
  144.  
  145. int
  146. main(argc,argv)
  147. int argc;
  148. char *argv[];
  149. {
  150.     int i, j, l, dif;
  151.     unsigned int v;
  152.     char *ch, *in, *out;
  153.     struct inrec wrk;
  154.  
  155.     in = out = NULL;
  156.     if (argc == 3) {
  157.         in = argv[1];
  158.         out = argv[2];
  159.     }
  160.     do {
  161.         if (in) /* Name supplied in command line */ {
  162.             fnami=fopen(in,"rt");
  163.             if (fnami == NULL) {
  164.                 printf("%s does not exist.\n",in);
  165.                 in = NULL;
  166.             }
  167.         } else {
  168.             printf("Symbol file to read? ");
  169.             getline(fname, 80);
  170.             fnami=fopen(fname,"rt");
  171.         }
  172.     } while (fnami == NULL);
  173.  
  174.     do {
  175.         if (out) /* Name supplied in command line */ {
  176.             fnamo=fopen(out,"wt");
  177.             if (fnamo == NULL) {
  178.                 printf("Can't write to %s\n",out);
  179.                 out = NULL;
  180.             }
  181.         } else {
  182.             printf("Symbol file to create? ");
  183.             getline(fname, 80);
  184.             fnamo=fopen(fname,"wt");
  185.         }
  186.     } while (fnamo == NULL);
  187.  
  188.     tabmax = 0;
  189.     for (i=0;i<SORTMAX;i++) {
  190.         sort_tab[i].val = 0;
  191.         sort_tab[i].name[0]=NULL;
  192.         key_tab[i] = -1;
  193.     }
  194.  
  195.     while (!feof(fnami)) {
  196.         l=fgetline(zarr,80,fnami);
  197.         if (l>0) {
  198.             ch = zarr;
  199.             key_tab[tabmax] = tabmax;
  200.             sort_tab[tabmax].val=gethex(&ch);
  201.             i=0;
  202.             if (*ch == 0x5f) ch++;
  203.             while (*ch && i<STRLEN) sort_tab[tabmax].name[i++] = *ch++;
  204.             while (i<STRMAX) sort_tab[tabmax].name[i++] = 0x20;
  205.             sort_tab[tabmax].name[STRMAX-1]=0;
  206.  
  207.             j = 0;    /* Note side effects of J */
  208.             for (i=0;i<tabmax;i++) /* Sort on to the list */ {
  209.                 dif = strcmp(sort_tab[key_tab[i]].name,
  210.                          sort_tab[key_tab[tabmax]].name);
  211.                 if (dif>0) {
  212.                     v = key_tab[tabmax];
  213.                     key_tab[tabmax] = key_tab[i];
  214.                     key_tab[i] = v;
  215.                     j = TRUE;
  216.                 }
  217.                 else if (!j && !dif) {
  218.                     if (sort_tab[key_tab[i]].val ==
  219.                         sort_tab[key_tab[tabmax]].val) {
  220.                         tabmax--;    /* Dupe */
  221.                         break;
  222.                     }
  223.                     printf("%s is not unique! (%s)\n", sort_tab[tabmax].name, zarr);
  224.                 }
  225.             }
  226.             if (++tabmax >= SORTMAX) {
  227.                 printf(" Symbol Table EOF at %d ! \n",tabmax);
  228.                 break;
  229.             }
  230.         }
  231.     }
  232.     fclose(fnami);
  233.  
  234.     printf("\nThere were %d Symbols read.\n\n",tabmax);
  235.  
  236.     zarr[0]=0;
  237.     for (j=0;j<tabmax;j++) {
  238.         i = key_tab[j];
  239.         if (70 < sprintf(zarr, "%s%4.4X %s ",
  240.                 zarr, sort_tab[i].val, sort_tab[i].name)) {
  241.             fputline(zarr,fnamo);
  242.             zarr[0]=0;
  243.         }
  244.     }
  245.     if (strlen(zarr) > 0) fputline(zarr,fnamo);
  246.  
  247.     /* All Done, close files */
  248.     fclose(fnamo);
  249.     return 0;
  250. }
  251.  
  252.